import { deleteByPrimaryKeyForProPost, insertForProPost, updateForProPost, getOneForProDept,getAllForProDept,queryPageForProPost,getProDeptTreeByParentid } from '@/services/system/api_propost';

export default {
    namespace: 'propost',

    state: {
        data: {
            list: [],
            pagination: {},
        },
        selectDeptTree: {
        	list:[],
        },
    },

    effects: {
            *delete_by_primarykey_for_propost({ payload,callback }, { call, put }) {
                const response = yield call(deleteByPrimaryKeyForProPost, payload);
								
				const newPayload = {currentPage: payload.currentPage,pageSize: payload.pageSize};
                const responsedata = yield call(queryPageForProPost, newPayload);
                yield put({
	                type: 'queryPageForProPost',
	                payload: responsedata,
                });
								
				if (callback) callback(response);

            },
            *insert_for_propost({ payload, callback }, { call, put }) {
                const response = yield call(insertForProPost, payload);

                const responsedata = yield call(queryPageForProPost, {});
                yield put({
	                type: 'queryPageForProPost',
	                payload: responsedata,
                });

                if (callback) callback(response);
            },
            *update_for_propost({ payload, callback }, { call, put }) {
                const response = yield call(updateForProPost, payload);
								
				const newPayload = {currentPage: payload.currentPage,pageSize: payload.pageSize};
                const responsedata = yield call(queryPageForProPost, newPayload);
                yield put({
	                type: 'queryPageForProPost',
	                payload: responsedata,
                });

                if (callback) callback(response);
            },
            *get_one_for_propost({ payload, callback }, { call, put }) {
                const response = yield call(getOneForProDept, payload);
                yield put({
                    type: 'getOneForProDept',
                    payload: response,
                });

                if (callback) callback();
            },
            *get_all_for_propost({ payload, callback }, { call, put }) {
                const response = yield call(getAllForProPost, payload);
                yield put({
                    type: 'getAllForProPost',
                    payload: response,
                });

                if (callback) callback();
            },
            *query_page_for_propost({ payload, callback }, { call, put }) {
                const response = yield call(queryPageForProPost, payload);
                yield put({
                    type: 'queryPageForProPost',
                    payload: response,
                });

                if (callback) callback();
            },
            *get_prodepttree_by_parentid({ payload, callback }, { call, put }) {
                const response = yield call(getProDeptTreeByParentid, payload);
                yield put({
                    type: 'getProDeptTreeByParentid',
                    payload: response,
                });

                if (callback) callback(response);
            },
    },

    reducers: {
        deleteByPrimaryKeyForProPost(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        insertForProPost(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        updateForProPost(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        getOneForProPost(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        getAllForProPost(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        queryPageForProPost(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        getProDeptTreeByParentid(state, action) {
            return {
                ...state,
                selectDeptTree: action.payload,
            };
        },
    },
};
